<template>
  <div class="p-channel table-box">
    <ProTable
      ref="proTable"
      title="momentList"
      :columns="columns"
      :request-api="getTableList"
      :init-param="initParam"
      :data-callback="dataCallback"
    >
      <!-- 表格操作 -->
      <template #operation="scope">
        <el-button type="primary" link @click="handleEditChannel(scope.row)"> 编辑 </el-button>
        <el-button type="primary" link @click="handleChangeStatus(scope.row)">
          {{ scope.row.status == 1 ? "下线" : "上线" }}
        </el-button>
      </template>
    </ProTable>
  </div>

  <!-- 图片预览 -->
  <PreviewImage ref="previewRef" />

  <!-- 编辑抽屉 -->
  <BannerEditDrawer ref="drawerRef" />
</template>

<script setup lang="tsx" name="channel">
import { ref, reactive } from "vue";
import { ProTableInstance, ColumnProps } from "@/components/ProTable/interface";
import PreviewImage from "@/views/proTable/components/PreviewImage.vue";
import { updateBanner, getMLBannerList, updateMLBanner } from "@/api/banner";
import { deepClone } from "@/utils/index";
import { ElMessage } from "element-plus";
import BannerEditDrawer from "./components/BannerEditDrawer.vue";

const proTable = ref<ProTableInstance>();
const initParam = reactive({});
const getTableList = async (option: any) => {
  return getMLBannerList();
};
const previewRef = ref<InstanceType<typeof PreviewImage> | null>(null);
const showImages = (row: any, index: number) => {
  const params = {
    index: index,
    row: row
  };
  previewRef.value?.previewParams(params);
};
const getOnOffStatus = () => {
  return [
    { stateLabel: "上线", stateValue: 1 },
    { stateLabel: "下线", stateValue: 2 }
  ];
};
const columns: ColumnProps[] = [
  {
    prop: "banner_url",
    label: "官网banner图",
    render: scope => {
      return (
        <el-image
          style="z-index:100;width:120px;cursor: pointer;"
          src={scope.row.banner_url}
          onClick={() => showImages(scope.row.banner_url, 0)}
        />
      );
    }
  },
  {
    prop: "title",
    label: "文字标题"
  },
  {
    prop: "status",
    label: "状态",
    enum: getOnOffStatus(),
    fieldNames: { label: "stateLabel", value: "stateValue" }
  },
  {
    prop: "operation",
    label: "操作",
    fixed: "right",
    width: "170"
  }
];
const dataCallback = (data: any) => {
  console.log(data);
  const bannerList = [];
  bannerList.push(data);
  //data目前是一个对象，需要转换成数组
  return {
    list: bannerList
  };
};

const drawerRef = ref<InstanceType<typeof BannerEditDrawer> | null>(null);

const handleEditChannel = (row: any) => {
  console.log(row);
  const params = {
    isView: false,
    row: { ...row },
    api: updateMLBanner,
    getTableList: proTable.value?.getTableList
  };
  const _params = deepClone(params); // 断一下引用
  _params.api = updateMLBanner;
  _params.getTableList = proTable.value?.getTableList;
  drawerRef.value?.acceptParams(_params);
};

const handleChangeStatus = async (row: any) => {
  console.log(row);
  try {
    const params = {
      banner_url: row.banner_url,
      cover_url: row.cover_url,
      title: row.title,
      status: row.status == 1 ? 2 : 1,
      content: row.content
    };
    const resp: any = await updateMLBanner(params);
    if (resp.code != 200) {
      return ElMessage.warning({ message: resp.msg });
    }
    ElMessage.success({ message: `${row.status == 1 ? "下线" : "上线"}成功！` });
    row.status = row.status == 1 ? 2 : 1;
  } catch (e) {
    ElMessage.warning({ message: (e as any).toString() });
  }
};
</script>

<style lang="scss" scoped>
.p-channel {
  ::v-deep img {
    object-fit: contain;
  }
}
</style>
